package com.njlime.cms.repository.app;

import com.njlime.cms.entities.InformationComment;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dean on 2017-7-27.
 */
@Repository
public class AppInformationCommentDaoImpl extends BaseDaoImpl<InformationComment> {
    /**
     * 保存资讯评论
     * @param informationComment
     * @return
     */
    public int saveComment(InformationComment informationComment) {
        StringBuffer sb = new StringBuffer();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("insert into t_information_comment ");
        sb.append("(information_id,comment,user_id,user_name,create_time) ");
        sb.append("values(?,?,?,?,?)");
        arrList.add(informationComment.getInformation_id());
        arrList.add(informationComment.getComment());
        arrList.add(informationComment.getUser_id());
        arrList.add(informationComment.getUser_name());
        arrList.add(informationComment.getCreate_time());
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 评论列表
     * @param pageBean
     * @param comment
     * @return
     */
    public PageBean list(PageBean pageBean, InformationComment comment) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select rc.*,v.head_url from t_information_comment rc, t_member v");
        sb.append(" where rc.is_del != 1");
        sb.append(" and rc.information_id = ?");
        sb.append(" and rc.user_id = v.id");
        sb.append(" and rc.status = 1");
        arrList.add(comment.getInformation_id());
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by rc.is_top desc, rc.create_time desc limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 点赞数增加
     * @param id
     */
    public void fabulousNumUpdate(Integer id, int num) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_information_comment SET fabulous_num = fabulous_num + ? WHERE id = ?");
        arrList.add(num);
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }
}
